<?php
require_once("../inc/constants.inc.php");
require_once("../inc/connection.php");
// define variables and set to empty values

if(isset($_POST['resetPassword']))
{	
	if (empty($_POST["email"]))
		{	 
			$url="../forgot-password.php?isEmpty=yes";
			header("Refresh:0;URL=$url");
		}
	elseif(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",test_input($_POST["email"])))       
		{     
			$url="../forgot-password.php?isWrong=yes";
			header("Refresh:0;URL=$url");   
		}
	else
		{
			$emailNotFound = "check";
			$sql="SELECT UserEmail FROM login_details";
			$result = mysql_query($sql);
			while($row = mysql_fetch_array($result))
				{
					if($row['UserEmail'] != $_POST["email"])
						{
							$emailNotFound = "true";							
						}
					else
						{
							$emailNotFound = "false";
							$subject = "New Password Information"; 
							$email = $_POST["email"]; //user/client email input
							$to = $email;
							$name = "SmartInterview";
							$defaultpassword1 = md5(rand(1000,10000));
							$defaultpassword = substr($defaultpassword1, 0, 5);
							$defaultpasswordmd5 = md5($defaultpassword);						
							$sql1="UPDATE login_details SET UserPassword='$defaultpasswordmd5' WHERE UserEmail='$email'";
							$result1=mysql_query($sql1);
				
							$body = "Hello! 

                                     Your Smarttest account password has been changed. And your new password is $defaultpassword

                                     Regards,
                                     Smartek21 Recruitment Team";
			
	  
							 require("../php-mailer/class.phpmailer.php");
							 $mail = new PHPMailer(); 
							 $mail->IsSMTP(); // send via SMTP
							 $mail->SMTPAuth = true; // turn on SMTP authentication
							 $mail->Username = "info.smartek21@gmail.com"; // SMTP username
							 $mail->Password = "Smarttest@21";
							 $mail->Port = "465";
							 $webmaster_email = "info.smartek21@gmail.com"; //Reply to this email ID
							 $email="$to"; // Recipients email ID
							 $name1="SmartInterview"; // Recipient's name
							 $mail->From = $webmaster_email;
							 $mail->FromName = $name;
							 $mail->AddAddress($email,$name1);
							 $mail->AddReplyTo($webmaster_email,$name);
							 $mail->WordWrap = 10000; // set word wrap
							 //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
							 //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
							 $mail->IsHTML(true); // send as HTML
							 $mail->Subject = $subject;
							 $mail->Body = "<br/>".$body."<br/>"; //HTML Body
							 //$mail->AltBody = "asset have been allocated to you"; //Text Body
							 if(!$mail->Send())
								{
									echo "Mailer Error: " . $mail->ErrorInfo;
									exit(0);
								}
							 else
								{         
								 $url="../login.php?isEmailSuccess=yes&email=$to";
								 header("Refresh:0;URL=$url");
								 exit(0);	
								}
						}
	  
				}
				
				
			if($emailNotFound=="true")
				{
					$url="../login.php?isFound=no&email=$_POST[email]";
					header("Refresh:0;URL=$url");
					exit(0);
				}
					
		}
}
function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>